package jh.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jh.ios.forecast.R;

import org.apache.cordova.FileManager;

import java.util.Date;
import java.util.List;

import jh.model.MModel;
import jh.utils.XDateUtils;

public class LotteryAdapter extends BaseAdapter {

    private Context context;
    private LayoutInflater inflater;
    private List<MModel> list;
    private Boolean hideIcon;
    public LotteryAdapter(Context context,List<MModel> list,Boolean hideIcon){
        this.context = context;
        this.list = list;
        this.hideIcon = hideIcon;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }
    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;
        if (convertView == null){
            viewHolder = new ViewHolder();
            convertView = inflater.inflate(R.layout.layout_open_item,null);
            viewHolder.iconImage = convertView.findViewById(R.id.icon);
            viewHolder.name = convertView.findViewById(R.id.name);
            viewHolder.preiod = convertView.findViewById(R.id.preiod);
            viewHolder.time = convertView.findViewById(R.id.time);
            viewHolder.images = convertView.findViewById(R.id.images);
            viewHolder.labels = convertView.findViewById(R.id.labels);
            viewHolder.valueText = convertView.findViewById(R.id.valueText);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder)convertView.getTag();
        }

        MModel model = list.get(position);
        if (hideIcon){
            viewHolder.iconImage.setVisibility(View.GONE);
            viewHolder.preiod.setText("第"+model.getLotteryPeriods()+"期");
        }else {
            viewHolder.preiod.setText("第"+model.getLotteryPeriods().substring(model.getLotteryPeriods().length() - 4,model.getLotteryPeriods().length())+"期");
        }




        int count = viewHolder.images.getChildCount();

        String[] numbers = model.getLotteryOpenNumber().split(",");
        String path = FileManager.getFilePath()+"/www/assets/gameIcon/";
        String imageName = "";
        for (int j=0;j<count;j++){
            viewHolder.images.getChildAt(j).setVisibility(View.GONE);
        }
        int sum = 0;
        for (int i=0;i<numbers.length;i++){
            TextView textView = (TextView)viewHolder.labels.getChildAt(i);
            ImageView view = (ImageView) viewHolder.images.getChildAt(i);
            view.setVisibility(View.VISIBLE);
            sum += Integer.parseInt(numbers[i]);
            if (model.getParentLotteryCategoryCode().equals("k3")){
                imageName = "ks"+ Integer.parseInt(numbers[i])+".png";
            }else if (model.getParentLotteryCategoryCode().equals("pk10")){
                imageName = "pk"+ Integer.parseInt(numbers[i])+".png";
            }else if (model.getParentLotteryCategoryCode().equals("lhc")){
                imageName = "lh"+ numbers[i]+".png";
                textView.setText(MModel.getType(numbers[i]));
            }else{
                imageName = Integer.parseInt(numbers[i])+".png";
            }

            Glide.with(context).load(path+imageName).into(view);
        }
        viewHolder.images.setPadding(0,0,0,0);
        viewHolder.images.setBackground(null);
        viewHolder.valueText.setVisibility(View.GONE);
        if (model.getParentLotteryCategoryCode().equals("k3")){

            String type = sum > 9 ? "大" : "小";
            String type1 = sum % 2 == 0 ? "双" : "单";

            viewHolder.valueText.setVisibility(View.VISIBLE);
            viewHolder.valueText.setText("和值："+sum+"  "+type+type1);
            viewHolder.images.setPadding(30,8,15,8);
            viewHolder.images.setBackground(context.getResources().getDrawable(R.drawable.ks_solid_round));
        }else if (model.getParentLotteryCategoryCode().equals("ssc")){
            viewHolder.valueText.setVisibility(View.VISIBLE);
            String type = sum >= 23 ? "大" : "小";
            String type1 = sum % 2 == 0 ? "双" : "单";
            viewHolder.valueText.setText(type+type1+" | "+MModel.nuinui(numbers));
        }



        if (model.getParentLotteryCategoryCode().equals("lhc")){
            viewHolder.labels.setVisibility(View.VISIBLE);
        }else {
            viewHolder.labels.setVisibility(View.GONE);
        }




        Glide.with(context).load(model.getLotteryCategoryLogoUrl()).into(viewHolder.iconImage);
        viewHolder.name.setText(model.getLotteryCategoryName());
        Date date = XDateUtils.string2Date(model.getLotteryOpenTime(),"yyyy-MM-dd HH:mm:ss");
        viewHolder.time.setText(XDateUtils.date2String(date,"MM-dd HH:mm"));
        return convertView;
    }


    public class ViewHolder{
        public ImageView iconImage;
        public TextView name;
        public TextView preiod;
        public TextView time;
        public LinearLayout images;
        public LinearLayout labels;
        public TextView valueText;
    }

}
